﻿Imports System.Web
Imports System.Web.Services
Imports System.IO
Public Class hndUpload
    Implements System.Web.IHttpHandler
    Private maxPhotoSize As Integer = CInt(ConfigurationManager.AppSettings("maxPhotoSize"))
    Private photoExt As String = ConfigurationManager.AppSettings("photoExt")
    Private maxPhotoNumber As Integer = CInt(ConfigurationManager.AppSettings("maxPhotoNumber"))
    Private photoPath As String = ConfigurationManager.AppSettings("photoPath")
    Private docsPath As String = ConfigurationManager.AppSettings("docsPath")
    Private ok As String = "{'success':true}"
    Private ko As String = "{'success':false}"
    Private cmnHelp As New Helpers.Common
    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            Dim adID As New Guid(context.Request("id"))
            'If photoExt.Contains(context.Request("qqfile").ToLower.Substring(context.Request("qqfile").LastIndexOf("."))) _
            'And context.Request.InputStream.Length <= maxPhotoSize Then
            If Not String.IsNullOrEmpty(context.Request("type")) And context.Request.InputStream.Length > 0 Then
                Select Case context.Request("type")
                    Case "img"
                        Dim adPath As String = photoPath & adID.ToString
                        Dim adPathFile As String = adPath & "/" & context.Request("qqfile") 'HttpFile.FileName
                        If Not Directory.Exists(context.Server.MapPath(adPath)) Then
                            Directory.CreateDirectory(context.Server.MapPath(adPath))
                        End If
                        Dim image As Drawing.Image = Drawing.Image.FromStream(context.Request.InputStream)
                        image.Save(context.Server.MapPath(adPathFile))
                        context.Response.Write(ok)
                    Case Else
                        Dim adPath As String = docsPath & adID.ToString
                        Dim adPathFile As String = adPath & "/" & context.Request("qqfile") 'HttpFile.FileName
                        If Not Directory.Exists(context.Server.MapPath(adPath)) Then
                            Directory.CreateDirectory(context.Server.MapPath(adPath))
                        End If
                        cmnHelp.SaveStreamToFile(context.Server.MapPath(adPathFile), context.Request.InputStream)
                        context.Response.Write(ok)
                End Select
            Else
                context.Response.Write(ko)
            End If
        Catch ex As Exception
            context.Response.Write(ko)
        End Try
    End Sub
    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class